package com.zxl.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @packageName com.zxl.vo
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";

    private String status; // 表示请求是否成功成功

    private String msg; // 如果请求失败，失败原因

    private T data; // 响应的数据

    /*
        成功时
     */
    public static R success() {
        return new R(SUCCESS, null, null);
    }

    public static R error(String msg) {
        return new R(ERROR, msg, null);
    }

    // 公共的响应方法
    public static R write(Object targer) {
        if (targer instanceof Boolean) {
            Boolean flag = (Boolean) targer;
            if (flag) {
                return success();
            } else {
                return error("操作失败");
            }
        }
        return error(null);
    }
}
